package com.crm.system.service.impl.taskManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.taskManager.TaskInfo;
import com.crm.model.qo.taskManager.TaskInfoQO;
import com.crm.model.vo.taskManager.TaskInfoVO;
import com.crm.model.vo.taskManager.TodayTaskVO;
import com.crm.service.taskManager.TaskInfoDbService;
import com.crm.system.service.taskManager.TaskInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author renshaorong
 * @Date 2021/4/25
 */
@Service
public class TaskInfoServiceImpl implements TaskInfoService {

    @Autowired
    private TaskInfoDbService taskInfoDbService;

    @Override
    public DhPageInfo<TaskInfoVO> getAllTaskInfoByAccountId(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, TaskInfoQO taskInfoQO) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        //增加删除状态查询条件(0:正常;1:已删除)
        taskInfoQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        taskInfoQO.setAccountId(((UserAccount) SecurityUtils.getSubject().getPrincipal()).getAccountId());

        StringBuilder order = new StringBuilder();
        if(!StringUtils.isBlank(sortName)&&!StringUtils.isBlank(sortOrder)){
            switch(sortName){
                case "createTm":
                {
                    order.append("a.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }

        List<TaskInfoVO> taskList  = taskInfoDbService.getAllTaskInfoByAccountId(taskInfoQO, order.toString());
        PageInfo<TaskInfoVO> pageInfo = new PageInfo<TaskInfoVO>(taskList);
        return new DhPageInfo<TaskInfoVO>(pageInfo, taskList);
    }

    /**
     * 保存任务
     * @param taskInfo
     * @return
     */
    @Override
    public boolean saveTaskInfo(TaskInfo taskInfo) {
        boolean result = false;
        if(StringUtils.isNotEmpty(taskInfo.getTaskId())){//非空时为编辑保存
            taskInfo.setUpdateTm(new Date());
            taskInfo.setUpdatePeopleId(((UserAccount) SecurityUtils.getSubject().getPrincipal()).getAccountId());
            result = taskInfoDbService.updateTaskInfo(taskInfo);
        }else{
            taskInfo.setTaskId(CommonUtils.getGUID());
            taskInfo.setCreateTm(new Date());
            taskInfo.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            taskInfo.setCreatePeopleId(((UserAccount) SecurityUtils.getSubject().getPrincipal()).getAccountId());
            result = taskInfoDbService.saveTaskInfo(taskInfo);
        }
        return result;
    }

    /**
     * 根据主键查询任务信息
     * @param taskId
     * @return
     */
    @Override
    public TaskInfo getTaskInfoByTaskId(String taskId) {
        return taskInfoDbService.getTaskInfoByTaskId(taskId);
    }

    /***
     * @Description:用户端-任务列表—本日任务
     * @Param:selectDate 查询日期
     * @Param:pageIndex/pageSize 页码/每页显示数量
     * @Author: luojie
     * @Date: 2021/4/26 18:08
     */
    @Override
    public DhPageInfo<TodayTaskVO> getTodayTask(String selectDate,Integer pageIndex, Integer pageSize) throws ParseException {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date date = simpleDateFormat.parse(selectDate);

        //登录人
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<TodayTaskVO> taskList  = taskInfoDbService.getTodayTask(userAccount.getAccountId(),date);
/*        List<TodayTaskVO> todayTaskVOList = new ArrayList<TodayTaskVO>();
        taskList.stream().forEach(taskInfo -> {
            TodayTaskVO todayTaskVO = new TodayTaskVO();
            BeanUtils.copyProperties(taskInfo, todayTaskVO);
            todayTaskVOList.add(todayTaskVO);
        });*/
        PageInfo<TodayTaskVO> pageInfo = new PageInfo<TodayTaskVO>(taskList);
        return new DhPageInfo<TodayTaskVO>(pageInfo, taskList);
    }
}
